package cn.darkhorse.device.service.impl;

import cn.darkhorse.common.config.WeatherConfig;
import cn.darkhorse.common.constant.WeatherComstant;
import cn.darkhorse.common.util.GZIPHttpUtils;
import cn.darkhorse.device.service.LargeDataViewService;
import cn.darkhorse.device.vo.MesWeatherVO;
import cn.darkhorse.device.vo.WeatherRangeVO;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class LargeDataViewServiceImpl implements LargeDataViewService {
    /**
     * @return
     */

    @Override
    public MesWeatherVO getWather(String location) {
        String param="location="+WeatherConfig.getLocation()+"&key="+WeatherConfig.getKey()+"&gzip=n";
        String url=WeatherComstant.weatherUrl+"?"+param;
        String weather = GZIPHttpUtils.doGet(url);
        JSONObject object=JSONObject.parseObject(weather);
        MesWeatherVO vo= JSONObject.parseObject(object.get("now").toString(), MesWeatherVO.class);
        String result = HttpUtils.sendGet(WeatherComstant.locationUrl,param);
        object= JSONObject.parseObject(result);
        JSONObject obj= (JSONObject) object.getJSONArray("location").get(0);
        if(null!=obj){
            MesWeatherVO vo1= JSONObject.parseObject(obj.toString(), MesWeatherVO.class);
            vo.setAdm1(vo1.getAdm1());
            vo.setAdm2(vo1.getAdm2());
            vo.setCountry(vo1.getCountry());
            vo.setName(vo1.getAdm2()+"市"+vo1.getName());
        }
        //param="location="+WeatherConfig.getLon()+","+WeatherConfig.getLat()+"&key="+WeatherConfig.getKey()+"&gzip=n";
        url=WeatherComstant.threeDayWeatherUrl+"?"+param;
        String grid=GZIPHttpUtils.doGet(url);
        if(JSONObject.parseObject(grid).containsKey("daily")) {
            JSONArray jsonArray = JSONArray.parseArray(JSONObject.parseObject(grid).get("daily").toString());
            JSONObject jsonObject = (JSONObject) jsonArray.get(0);
            if (null != jsonObject) {
                WeatherRangeVO weatherRangeVO=JSONObject.parseObject(jsonObject.toString(), WeatherRangeVO.class);
                vo.setTempMax(weatherRangeVO.getTempMax());
                vo.setTempMin(weatherRangeVO.getTempMin());
                vo.setWindScaleDay(weatherRangeVO.getWindScaleDay());
                vo.setWindScaleNight(weatherRangeVO.getWindScaleNight());
            }
        }
        return vo;
    }
}
